/**
 * @module NavBar
 * @class NavBar
 */
define(function(require, exports, module) {
    
    function NavBar(opt) {
        if (!(this instanceof NavBar)) {
            return new NavBar(opt);
        }

        this.searchBefore = true;
        this.setPageState = true; //true显示分类页 false 显示搜索页
        
        this.isShowCloseBtn = false;
        
        this.timer = true;
        
    }

    NavBar.prototype = {
        /**
         * 初始化
         * @mehod init
         */
        init: function () {
            console.log('已调用navbar');
            
            this.setPageNav();
            this.isShowPage();
            //导航点击事件
            this.clickNavPageEvent();
            //导航输入事件监听
            this.inputEvent();

            this.renderCloseBtn();

            this.changePageSearch();
            //点击关闭按扭事件
            this.clickCloseEvent();
        },
        clickCloseEvent: function() {
            

            var iconClose = $('.icon-close');
            var navBarSearchInput = $('.navBar-search input');
            var that = this;
            iconClose.off('tap').on('tap', function () {
                that.isShowCloseBtn = false;

                console.log(navBarSearchInput, 'navBarSearchInputnavBarSearchInput');
                navBarSearchInput.val('');
                $(this).hide();
                that.renderCloseBtn();
                that.changePageSearch();
            })
            
           
            
            
        },
        /**
         * 设置页面导航显示状态
         * @method setPageNav
         */
        setPageNav: function () {

            var navBarSearch = $('.navBar-search');
            var iconClose = $('.icon-close');
            var iconSearchBtn = $('.icon-searchBtn');
            var navBarMore = $('.navBar-more');

            if (this.searchBefore) {

                navBarSearch.removeClass('clickAfter');
                navBarSearch.addClass('clickBefore');

                // iconClose.hide();
                iconSearchBtn.hide();
                navBarMore.show()

            } else {
                navBarSearch.removeClass('clickBefore');
                navBarSearch.addClass('clickAfter');

                // iconClose.show();
                iconSearchBtn.show();
                navBarMore.hide()
            }

        },
        /**
         * 是否显示页面
         * @method isShowPage
         */
        isShowPage: function() {
            var pageBody = $('.page-body');
            var pageSearchBody = $('.page-search-body');

            // this.isShowCloseBtn = true;


            if (this.setPageState) {
                pageBody.show();
                pageSearchBody.hide();
                
                this.searchBefore = true;
             
            } else {
                pageBody.hide();
                pageSearchBody.show();
                
                this.searchBefore = false;
           
            }
            this.setPageNav();
        },
        clickNavPageEvent: function() {
            var navBarSearch = $('.navBar-search');
            var navBarBack = $('.navBar-back');

            // this.isShowCloseBtn = true;
            // this.renderCloseBtn();
            navBarSearch.off('tap').on('tap', function () {
                console.log('inputEvent<--');
                this.setPageState = false;
                this.searchBefore = false;

                this.isShowPage();
                
                
            }.bind(this));

            navBarBack.off('tap').on('tap', function () {
                this.setPageState = true;
                this.searchBefore = true;
                $('.navBar-search input').val('');
                $('.icon-close').hide();
                this.isShowPage();
            }.bind(this))

            

        },
        inputEvent: function() {
            var navBarSearchInput = $('.navBar-search input');
            var debounceInpput = this.debounce(this.searchContentShow, 1000);
         
            navBarSearchInput.off('keyup').on('keyup', function (e) {
               
                console.log(e.target.value, 'e.target.value');
                if (e.target.value) {
                    this.isShowCloseBtn = true;
                    debounceInpput(e.target.value);
                } else {
                    this.isShowCloseBtn = false;
                }
                // 控制关闭按扭显示
                this.renderCloseBtn();
                // 控制页面切换
                this.changePageSearch();

                

                // this.searchContentShow(e.target.value);
            }.bind(this));
        },
        searchContentShow: function (keyword) {
            // http://youyong.ba:8080/mock/5fed6c290d6d9c4f58824008/title
            var html = '';
        
            

        
            $.ajax({
                url: "http://youyong.ba:8080/mock/5fed6c290d6d9c4f58824008/title?keyword=" + keyword,
                success:function (res) {
                    console.log(res, '');
                    for (var i=0; i< res.data.length; i++) {
                        html += '<li class="hasData-item">'+ res.data[i].title+'</li>'
                    }
                    $('.hasData').html(html);
                }
            });
        },

        debounce: function (fun, delay) {
            // var timer;
                return function (args) {
                    var that = this;
    
                    clearTimeout(this.timer);
    
                    this.timer = setTimeout(function () {
                        fun.call(that, args)
                    },delay)
                }
        },


        // function debounce(fun, delay) {
        //     var timer;
        //     return function (args) {
        //         var that = this;

        //         clearTimeout(timer);

        //         timer = setTimeout(function () {
        //             fun.call(that, args)
        //         },delay)
        //     }
        // }

        // var debounceInpput = debounce(input, 1000);

        // //监听事件按键抬起
        // $('#ipt').off("keyup").on("keyup", function (e) {
            
        //     debounceInpput(e.target.value);
        // });

        changePageSearch: function() {
            var noData = $('.noData');
            var hasData = $('.hasData');
            
            if (this.isShowCloseBtn) {
                hasData.show();
                noData.hide();
            } else {
                hasData.hide();
                noData.show();
            }
            
        },
        renderCloseBtn:function() {
           
            console.log(1);
            if (this.isShowCloseBtn) {
                console.log(2);
                $('.icon-close').show();
            } else {
                console.log(3);
                $('.icon-close').hide();
                
            }

            
        }
    }

    module.exports = NavBar;
    
});